/*
 * Copyright (c) 2009-2013 EvTech Project
 * All rights reserved.
 * 
 * This file is part of LModeller.
 *
 * LModeller is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 *
 * LModeller is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LModeller.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package fi.honkalampisaatio.lmodeller.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import org.lwjgl.util.Rectangle;

import fi.honkalampisaatio.lmodeller.LModeller;
import fi.honkalampisaatio.lmodeller.LanguageResource;
import fi.honkalampisaatio.lmodeller.Logger;
import fi.honkalampisaatio.lmodeller.ModelViewPage;
import fi.honkalampisaatio.lmodeller.blocks.BlockBox;
import fi.honkalampisaatio.lmodeller.blocks.BlockBuilder;
import fi.honkalampisaatio.lmodeller.blocks.BlockModel;
import fi.honkalampisaatio.lmodeller.selection.ModelSelectionEvent;
import fi.honkalampisaatio.lmodeller.selection.GameModeSelectionEvent.MODES;
import glapp.GLApp;

public class RandomNumberTableMenu extends RandomMenu implements ActionListener {
	ArrayList<GameTextLabel> numberButtons;
	int blockCount;
	int maxBlockCount, minBlockCount, columnCount;
	
	public RandomNumberTableMenu(int minBlockCount, int maxBlockCount, int columnCount)
	{
		this.minBlockCount = minBlockCount;
		this.maxBlockCount = maxBlockCount;
		this.columnCount = columnCount;
		numberButtons = new ArrayList<GameTextLabel>();
	}
	
    public void init()
    {
    	int rowHeight, rowGap, columnWidth, columnGap;
    	int h = GLApp.getHeight()/10;
    	int w = GLApp.getWidth()/10;
    	int allBlocksCount = BlockBox.getBlockBox().getCopyofAllBlocks().size();
    	maxBlockCount = (maxBlockCount>allBlocksCount)?allBlocksCount:maxBlockCount;
    	minBlockCount = (minBlockCount>maxBlockCount)?maxBlockCount:minBlockCount;
    	blockCount = maxBlockCount - minBlockCount + 1;
    	columnWidth = (int)(w*8*0.9f/columnCount);
    	columnGap = (int)(w*8*0.1f/columnCount);
    	rowHeight = (int)(h*8*0.9f/Math.ceil(blockCount/(double)columnCount));
    	rowGap = (int)(h*8*0.1f/Math.ceil(blockCount/(double)columnCount));
    	
    	for(int y=0;y*columnCount<blockCount;y++)
    	{
        	for(int x=0;y*columnCount+x<blockCount && x<columnCount;x++)
        	{
        		GameTextLabel nbrLabel = new GameTextLabel(new Rectangle(w+(columnWidth+columnGap)*x,
        				h*9-rowHeight-(rowHeight+rowGap)*y,columnWidth,rowHeight),
            			(int)(Math.min(columnWidth, rowHeight)*0.8), LModeller.actionColor);
        		String text = String.valueOf(y*columnCount+x+minBlockCount);
        		nbrLabel.setText(text);
        		nbrLabel.showFrame(true);
        		numberButtons.add(nbrLabel);
        		nbrLabel.setToolTipSentence(text);
            	super.addComponent(nbrLabel);
            	nbrLabel.addActionListener(this);
        	}
    		
    	}

    	mode = MODES.RANDOM_STEP_GAME;

    	setUpLabelText(LanguageResource.getInstance().get("CHOOSE_BLOCK_COUNT"));
    	

    	setUpToolTipSentence(LanguageResource.getInstance().get("CHOOSE_BLOCK_COUNT"));
    	setInstructionSentence(LanguageResource.getInstance().get("CHOOSE_BLOCK_COUNT"));
    }

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() !=null && e.getSource() instanceof GameTextLabel)
		{
			GameTextLabel nbrButton =  (GameTextLabel)e.getSource();
			BlockModel model = null;
			ArrayList<ModelViewPage> pages = new ArrayList<ModelViewPage>();
			try {
				int currentBlockCount = Integer.parseInt(nbrButton
						.getText());
				model = BlockBuilder.CreateRandomModel(currentBlockCount);
			} catch (Exception exception) {
				exception.printStackTrace();
				return;
			}
			switch(mode)
			{
			case RANDOM_STEP_GAME:
				pages.add(new ModelViewPage(model, LanguageResource.getInstance().get("LOOK")));
				for(int i=0;i<model.getBlocks().size();i++)
				{
					BlockModel subModel = new BlockModel();
					//subModel.setName(model.getName());
					for(int idx = 0; idx<=i; idx++)
					{
						subModel.addBlock(model.getBlocks().get(idx));
					}
					subModel.processModel();
					pages.add(new ModelViewPage(subModel, LanguageResource.getInstance().get("BUILD") + " " + (i+1) + " / " 
							+ model.getBlocks().size(), LanguageResource.getInstance().get("BUILD")));
				}
				pages.add(new ModelViewPage(model, LanguageResource.getInstance().get("READY")));
				break;
			case MEMORY_GAME:
				pages.add(new ModelViewPage(model, LanguageResource.getInstance().get("MEMORIZE")));
				pages.add(new ModelViewPage(null, LanguageResource.getInstance().get("BUILD")));
				pages.add(new ModelViewPage(model, LanguageResource.getInstance().get("CHECK")));
				break;
			default:
				pages.add(new ModelViewPage(model, LanguageResource.getInstance().get("BUILD")));
				break;
			}
			ModelSelectionEvent se = new ModelSelectionEvent(this, 
					pages);
			Logger.getInstance().log("RandomNumberTableMenu: Number button selected, block count is " + model.getBlocks().size());
			this.fireSelectionEvent(se);
		}
	}
    
}

